package com.woniu.inlet.subscrible;


import com.rabbitmq.client.Channel;
import com.woniu.outlet.dao.InventoryInoutPoMapper;
import com.woniu.outlet.dao.MaterialBomPoMapper;
import com.woniu.outlet.po.InventoryInoutPo;
import com.woniu.outlet.po.MaterialBomPo;
import com.woniu.outlet.po.MaterialBomPoExample;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/***
 *配件工作流程的出入库记录
 */
@Component
@Slf4j
public class PhApplicationListener {
    @Autowired
    private InventoryInoutPoMapper inventoryInoutPoMapper;

    @Autowired
    private MaterialBomPoMapper materialBomPoMapper;

    /**
     * 用来记录每个流程的配件流通
     * @param message
     * @param channel
     * @throws IOException
     */
    @RabbitListener(queues ="PHApplication_queue")
    public void getMessage(Message message, Channel channel) throws IOException {
        try {
            String str= new String(message.getBody());
            if(StringUtils.isEmpty(str)){
                return;
            }
            String[] splits1 = str.split("-");
            MaterialBomPoExample materialBomPoExample = new MaterialBomPoExample();
            MaterialBomPoExample.Criteria criteria = materialBomPoExample.createCriteria();
            criteria.andMaterialCodeEqualTo(splits1[0]);
            List<MaterialBomPo> materialBomPos = materialBomPoMapper.selectByExample(materialBomPoExample);
            MaterialBomPo materialBomPo=materialBomPos.get(0);
            InventoryInoutPo inventoryInoutPo = new InventoryInoutPo();
            Integer i = Integer.parseInt(materialBomPo.getId()+"");
            inventoryInoutPo.setMaterialBomId(i);
            inventoryInoutPo.setCode(materialBomPo.getMaterialCode());
            inventoryInoutPo.setColor(materialBomPo.getColor());
            inventoryInoutPo.setName(materialBomPo.getMaterialName());
            inventoryInoutPo.setInoutWay("people");
            inventoryInoutPo.setIsout("Y");
            inventoryInoutPo.setInoutNum(Integer.parseInt(splits1[1]));
            inventoryInoutPo.setSpecificationModel(materialBomPo.getSpecificationModel());
            inventoryInoutPo.setSpecificationUnit(materialBomPo.getSpecificationUnit());
            inventoryInoutPo.setTheoreticalWeight(materialBomPo.getTheoreticalWeight());
            inventoryInoutPo.setValuationUnit(materialBomPo.getValuationUnit());
            inventoryInoutPo.setType("C");
            inventoryInoutPo.setCreateTime(new Date());

            inventoryInoutPo.setCreateWorkerId(1);
            inventoryInoutPo.setInoutType("C");
            inventoryInoutPoMapper.insertSelective(inventoryInoutPo);
        }catch(Exception ex){
            //如果多次插入userid相同 即失败了 捕获异常  达到去重效果，return 返回， 后面的优惠券业务不再处理
            // 拒绝消息。要求 RabbitMQ 从队列中删除。注意参数为false
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
            return ;
        }



    }

}
